using GameFramework;
using Unity.VisualScripting;
using UnityEngine;
using UnityGameFramework.Runtime;

namespace LS.UGFUnits
{
    [UnitShortTitle("Data Node")]
    [UnitSurtitle("Check Existed")]
    public sealed class CheckDataNodeExisted : DataNodeUnit
    {
        protected override bool UseOutput => false;

        [DoNotSerialize]
        public ControlOutput Existed { get; private set; }
        
        [DoNotSerialize]
        public ControlOutput NotExisted { get; private set; }
        
        [DoNotSerialize]
        [PortLabelHidden]
        public ValueInput Path { get; private set; }
        
        protected override void Definition()
        {
            base.Definition();
            Existed = ControlOutput(nameof(Existed));
            NotExisted = ControlOutput(nameof(NotExisted));
            Path =  ValueInput<string>(nameof(Path), string.Empty);
            Requirement(Path, Input);
        }

        protected override ControlOutput In(Flow flow)
        {
            string path = flow.GetValue<string>(Path);
            var node = Target?.GetNode(path);
            if (node != null && node.GetData() != null)
            {
                return Existed;
            }
            return NotExisted;
        }
    }
}